package com.bloveoacom.util;


import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.support.v4.content.ContextCompat;
import android.text.Layout;
import android.text.SpannableStringBuilder;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.blankj.utilcode.util.SpanUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.bloveoacom.base.AppContext;
import com.bloveoacom.base.UserInfoBean;
import com.bloveoacom.weichi.R;
import com.bloveoacom.weichi.bean.NewsPostBean;
import com.bloveoacom.weichi.bean.OptionBean;
import com.bloveoacom.weichi.bean.RewardBean;
import com.bloveoacom.weight.ClickImageView;
import com.bloveoacom.weight.MultiImageView;
import com.chad.library.adapter.base.BaseViewHolder;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.File;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.List;

import static com.blankj.utilcode.util.ActivityUtils.startActivity;
import static com.bloveoacom.util.CalendarUtil.getCalendar;
import static com.bloveoacom.util.CalendarUtil.getTime2;
import static com.bloveoacom.util.NumberUtil.getIntByStr;


/**
 * Created by yjl on 2018/5/29.
 */

public class DataUtil {
    public static boolean isValidEOSAccount(String input) {
        String regex = "([a-z]|[1-5]){12}";
        return input.matches(regex);
    }
        public static int getResOne(int resstate){
            int res = R.mipmap.artboard_up_icon;
            switch (resstate) {
                case 1:
                    res = R.mipmap.artboard_up_icon;
                    break;
                case 2:
                    res = R.mipmap.artboard_down_icon;
                    break;
                case 0:
                    res = R.mipmap.artboard_icon;
                    break;
            }
            return  res;
        }
    public static int getColOne(int resstate){
        int res = R.color.color_normal;
        switch (resstate) {
            case 1:
            case 2:
                res = R.color.colorPrimary;
                break;
            case 0:
                res = R.color.color_normal;
                break;
        }
        return  res;
    }



    public static List<String> getTags(){
        List<String> datas = new ArrayList<>();
        datas.add("基础链");
        datas.add("货币");
        datas.add("支付");
        return  datas;
    }




    /**
     * 分享图片到 微信
     * @param
     */
    public static void shareWeChat(File file, int type){
        Uri uriToImage = Uri.fromFile(file);
        Intent shareIntent = new Intent();
        //发送图片到朋友圈
        ComponentName comp = null;
        if(type==0){
            comp = new ComponentName("com.tencent.mm", "com.tencent.mm.ui.tools.ShareToTimeLineUI");
        }else{
             comp = new ComponentName("com.tencent.mm", "com.tencent.mm.ui.tools.ShareImgUI");
        }

        //发送图片给好友。
        shareIntent.setComponent(comp);
        shareIntent.setAction(Intent.ACTION_SEND);
        shareIntent.putExtra(Intent.EXTRA_STREAM, uriToImage);
        shareIntent.setType("image/jpeg");
        startActivity(Intent.createChooser(shareIntent, "分享图片"));
    }

//    public static void sharePicToWx(IWXAPI api,File file,int type){
//        String path =file.getPath();
//        WXImageObject imgObj = new WXImageObject();
//        imgObj.setImagePath(file.getPath());
//
//        WXMediaMessage msg = new WXMediaMessage();
//        msg.mediaObject = imgObj;
//
//        Bitmap bmp = BitmapFactory.decodeFile(path);
//        Bitmap thumbBmp = Bitmap.createScaledBitmap(bmp, 150, 150, true);
//        bmp.recycle();
//        msg.thumbData = bmpToByteArray(thumbBmp, true);
//
//        SendMessageToWX.Req req = new SendMessageToWX.Req();
//        req.transaction = buildTransaction("img");
//        req.message = msg;
//        req.scene = type==0?SendMessageToWX.Req.WXSceneSession:SendMessageToWX.Req.WXSceneTimeline;
//        api.sendReq(req);
//    }
//
//    public static String buildTransaction(final String type) {
//        return (type == null) ? String.valueOf(System.currentTimeMillis()) : type + System.currentTimeMillis();
//    }
//    public static byte[] bmpToByteArray(final Bitmap bmp, final boolean needRecycle) {
//        ByteArrayOutputStream output = new ByteArrayOutputStream();
//        bmp.compress(Bitmap.CompressFormat.PNG, 100, output);
//        if (needRecycle) {
//            bmp.recycle();
//        }
//
//        byte[] result = output.toByteArray();
//        try {
//            output.close();
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//
//        return result;
//    }

    public  static  final  int PL=0;
    public  static  final  int DZ=1;
    public  static  final  int QD=2;
    public  static  final  int FT=3;
    public  static  final  int HT=4;
    public  static  final  int ZC=5;
    public  static  final  int FX=6;
    public  static  final  int YQ=7;
    public static SpannableStringBuilder getReward(Context context, int type, String num){
        CharSequence data1="",data2="",data3="",data4="";
        switch (type) {
            case 0://评论
                data1 = "币神，评论成功！";
                break;
            case 1://点赞
                data1 = "英雄，点赞成功！";
                break;
            case 2:
                data1 = "每日一签，好运常来！";
                break;
            case 3:
                data1 = "发帖成功，你太有才了！";
                break;
            case 4:
                data1 = "你的颜值好高,";
                break;
            case 5:
                data1 = "骚年，欢迎加入币吧！";
                break;
            case 6:
                data1 = "分享的人运气不会太差！";
                break;
            case 7:
                data1 = "邀请好友成功！好人缘！";
                break;
        }
        data2 = "\n获得";
        data3 = num+"TU";
        data4 = "奖励";
        return  new SpanUtils().appendLine(data1).setFontSize(16,true).setForegroundColor(ContextCompat.getColor(context,R.color.gray_text_color)).appendImage(R.drawable.ic_tu2)
                .append(data2)
                .setFontSize(14,true).setForegroundColor(ContextCompat.getColor(context,R.color.white))
                .append(data3)
                .setFontSize(14,true).setForegroundColor(ContextCompat.getColor(context,R.color.orange_text_color))
                .append(data4)
                .setFontSize(14,true).setForegroundColor(ContextCompat.getColor(context,R.color.white))
                .create();
    }

    //getString(R.string.has_comment)
    public static void showMsg(Context mContext,RewardBean rewardCommentBean,int resd,String remind,int msgType){
        ToastUtil.makeShortText(mContext,remind);
//        View view = ToastUtils.showCustomLong(R.layout.layout_reward);
//        TextView tvContent = view.findViewById(R.id.tv_content);
//        if (rewardCommentBean.getRewardTUnum() != 0) {
//            tvContent.setText(getReward(mContext, msgType, rewardCommentBean.getRewardTUnum() + ""));
//        } else {
//            if (StringUtils.isEmpty(UserInfoBean.getInstance().getWalletAccount())) {
//                tvContent.setText(mContext.getString(R.string.has_comment_no_wallet));
//            } else {
//                tvContent.setText(remind);
//            }
//            if(resd!=-1)
//             DisplayUtil.setDrawbleTop(mContext, tvContent, resd, 10);
//        }
    }




    public static SpannableStringBuilder get(Context context, String data){
        return  new SpanUtils().appendImage(R.drawable.ic_tu2)
                .append(data).setAlign(Layout.Alignment.ALIGN_CENTER)
                .setFontSize(14,true).setForegroundColor(ContextCompat.getColor(context,R.color.white))
                .setAlign(Layout.Alignment.ALIGN_CENTER)
                .create();
    }



    public static void setItemView(Context context, BaseViewHolder helper, NewsPostBean item){
        GlideUtil.loadCircleImage(helper.getView(R.id.iv_head), StringUtils.getFullUrl(item.getHeaderImg(), 0));
        helper.setText(R.id.tv_username, item.getUserName());
        if(StringUtils.isEmpty(item.userIdentity)||item.userIdentity.equals("0")){
            helper.setVisible(R.id.iv_head_icon,false);
        }else{
            helper.setVisible(R.id.iv_head_icon,true);
            switch (item.userIdentity) {
                case "1":
                    helper.setImageResource(R.id.iv_head_icon,R.drawable.ic_v_yellow);
                    break;
                case "2":
                    helper.setImageResource(R.id.iv_head_icon,R.drawable.ic_v_blue);
                    break;
            }
        }

       // helper.setVisible(R.id.iv_jx,getIntByStr(item.newsType)!=0);
        helper.setText(R.id.tv_time,   getTime2(item.getCreateTime(),3));
        helper.setText(R.id.tv_title, item.getTitle());
        helper.setText(R.id.tv_comment_count, String.valueOf(item.getCommentnum()));
        helper.setText(R.id.tv_tu_num, context.getString(R.string.tu_num, item.getTuNum()));
        helper.setText(R.id.tv_like_count, String.valueOf(item.getGiveLikeNum()));
        // helper.addOnClickListener(R.id.tv_comment_count);
        //helper.addOnClickListener(R.id.tv_like_count);
        helper.addOnClickListener(R.id.iv_head);
        helper.getView(R.id.tv_like_count).setSelected(NumberUtil.getIntByStr(item.getIsGiveLike()) == 1);
        MultiImageView pics = helper.getView(R.id.iv_pics);
        String images = item.images;
        if(StringUtils.isEmpty(images)){
            pics.setVisibility(View.GONE);
            helper.setVisible(R.id.tv_content,true);
            helper.setText(R.id.tv_content,item.getContent());
        }else{
            if(images.contains(",")){
                pics.setVisibility(View.VISIBLE);
                helper.setVisible(R.id.tv_content,false);
                helper.setText(R.id.tv_content,"");
                String[] split = images.split(",");
                pics.setList(Arrays.asList(split));
            }else{
                if(!StringUtils.isEmpty(images)){
                    helper.setVisible(R.id.tv_content,false);
                    pics.setVisibility(View.VISIBLE);
                    helper.setText(R.id.tv_content,"");
                    List<String> pic = new ArrayList<>();
                    pic.add(images);
                    pics.setList(pic);
                }else{
                    pics.setVisibility(View.GONE);
                    helper.setVisible(R.id.tv_content,true);
                    helper.setText(R.id.tv_content,item.getContent());
                }

            }
        }
    }

    public static void setItemView2(Context context, BaseViewHolder helper, NewsPostBean item,int type){
        helper.setText(R.id.tv_username, item.getUserName());
        helper.setText(R.id.tv_title, item.getTitle());
        MultiImageView pics = helper.getView(R.id.iv_pics);
        String images = item.images;
        if(StringUtils.isEmpty(images)){
            pics.setVisibility(View.GONE);
            helper.setVisible(R.id.tv_content,true);
            helper.setText(R.id.tv_content,item.getContent());
        }else{
            if(images.contains(",")){
                pics.setVisibility(View.VISIBLE);
                helper.setVisible(R.id.tv_content,false);
                helper.setText(R.id.tv_content,"");
                String[] split = images.split(",");
                pics.setList(Arrays.asList(split));
            }else{
                if(!StringUtils.isEmpty(images)){
                    helper.setVisible(R.id.tv_content,false);
                    pics.setVisibility(View.VISIBLE);
                    helper.setText(R.id.tv_content,"");
                    List<String> pic = new ArrayList<>();
                    pic.add(images);
                    pics.setList(pic);
                }else{
                    pics.setVisibility(View.GONE);
                    helper.setVisible(R.id.tv_content,true);
                    helper.setText(R.id.tv_content,item.getContent());
                }
            }
        }
        ImageView view = helper.getView(R.id.iv_head);
        switch (type) {
            case 0:
            case 2:
                GlideUtil.loadCircleImage(view, StringUtils.getFullUrl(item.headImg, 0));
                helper.setText(R.id.tv_time,  "最新评论:"+ item.lastCommentTime);
                helper.setText(R.id.tv_tu_num,String.valueOf(item.emtNum));
                helper.setText(R.id.tv_like_count,item.nowEmtNum);
                helper.setText(R.id.tv_comment_count,item.commentNum);
                break;
            case 1:
                helper.setText(R.id.tv_time,   item.getCreateTime());
                GlideUtil.loadCircleImage(view, StringUtils.getFullUrl(item.headImg, 0));
                break;
            case 3:
                helper.setText(R.id.tv_tu_num,String.valueOf(item.emtNum));
                GlideUtil.loadCircleImage(view, StringUtils.getFullUrl(item.headImg, 0));
                helper.setText(R.id.tv_time,  item.createTime);
                break;
            case 4:
                GlideUtil.loadCircleImage(view, StringUtils.getFullUrl(item.headImg, 0));
                helper.setText(R.id.tv_time,   item.lastCommentTime);
                helper.setText(R.id.tv_tu_num,String.valueOf(item.emtNum));
                helper.setText(R.id.tv_like_count,item.nowEmtNum);
                helper.setText(R.id.tv_comment_count,item.commentNum);
                break;
        }

        if(view instanceof ClickImageView){
            ((ClickImageView) helper.getView(R.id.iv_head)).setData(new OptionBean(item.userId));
        }
    }
    public static void setItemView(Context context, BaseViewHolder helper, NewsPostBean item,int type){
        GlideUtil.loadCircleImage((ImageView) helper.getView(R.id.iv_head), StringUtils.getFullUrl(item.getHeaderImg(), 0));
        helper.setText(R.id.tv_username, item.getUserName());
        if(StringUtils.isEmpty(item.userIdentity)||item.userIdentity.equals("0")){
            helper.setVisible(R.id.iv_head_icon,false);
        }else{
            helper.setVisible(R.id.iv_head_icon,true);
            switch (item.userIdentity) {
                case "1":
                    helper.setImageResource(R.id.iv_head_icon,R.drawable.ic_v_yellow);
                    break;
                case "2":
                    helper.setImageResource(R.id.iv_head_icon,R.drawable.ic_v_blue);
                    break;
            }
        }

      //  helper.setVisible(R.id.iv_jx,getIntByStr(item.newsType)!=0);
        helper.setText(R.id.tv_time,   getTime2(item.getCreateTime(),3));
        helper.setText(R.id.tv_title, item.getTitle());

        MultiImageView pics = helper.getView(R.id.iv_pics);
        String images = item.images;
        if(StringUtils.isEmpty(images)){
            pics.setVisibility(View.GONE);
            helper.setVisible(R.id.tv_content,true);
            helper.setText(R.id.tv_content,item.getContent());
        }else{
            if(images.contains(",")){
                pics.setVisibility(View.VISIBLE);
                helper.setVisible(R.id.tv_content,false);
                helper.setText(R.id.tv_content,"");
                String[] split = images.split(",");
                pics.setList(Arrays.asList(split));
            }else{
                if(!StringUtils.isEmpty(images)){
                    helper.setVisible(R.id.tv_content,false);
                    pics.setVisibility(View.VISIBLE);
                    helper.setText(R.id.tv_content,"");
                    List<String> pic = new ArrayList<>();
                    pic.add(images);
                    pics.setList(pic);
                }else{
                    pics.setVisibility(View.GONE);
                    helper.setVisible(R.id.tv_content,true);
                    helper.setText(R.id.tv_content,item.getContent());
                }

            }
        }
            switch (type) {
            case 1:
                break;
                default:
                    helper.setText(R.id.tv_comment_count, String.valueOf(item.getCommentnum()));
                    helper.setText(R.id.tv_tu_num, context.getString(R.string.tu_num, item.getTuNum()));
                    helper.setText(R.id.tv_like_count, String.valueOf(item.getGiveLikeNum()));
                    // helper.addOnClickListener(R.id.tv_comment_count);
                    //helper.addOnClickListener(R.id.tv_like_count);
                    helper.addOnClickListener(R.id.iv_head);
                    helper.getView(R.id.tv_like_count).setSelected(NumberUtil.getIntByStr(item.getIsGiveLike()) == 1);
                    break;
        }

    }


    public static String getSignature(String signature){
        if(StringUtils.isEmpty(signature)){
            return "这家伙很懒,什么都没有留下";
        }else{
            return signature;
        }
    }

    public static String getSignature2(String signature){
        if(StringUtils.isEmpty(signature)){
            return "查看个人主页和编辑资料";
        }else{
            return signature;
        }
    }
}
